// 监听麻将房推送
import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {MjListenRubyDeskComp} from "db://assets/script/game/majiang/comp/listenComp";
import {routersKey} from "db://assets/script/network/ws/router";
import {smc} from "db://assets/script/game/common/singletonModuleComp";
import {oops} from "db://oops-framework/core/Oops";
import {openDialogNotify, UIID} from "db://assets/script/game/common/config/gameUiConfig";
import {pb} from "db://assets/script/network/ws/proto";
import {Player} from "db://assets/script/game/player/player";
import {MjDeskView} from "db://assets/script/game/majiang/view/mjDeskView";
import {getNewTakeTileFromDuanPai} from "db://assets/script/utils/protocolUtils";

export class ListenMjSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(MjListenRubyDeskComp);
    }

    entityEnter(entity: ecs.Entity) {
        if (entity.has(MjListenRubyDeskComp)) {
            const comp = entity.get(MjListenRubyDeskComp)
            // 要监听金豆房
            this.listenRubyDesk(comp)
        }
    }


    listenRubyDesk(comp: MjListenRubyDeskComp) {
        // 游戏未开始，房间解散
        comp.listenRoute(routersKey.deskDissolvePush, (resp: pb.IDeskDissolvePushResp) => {
            const okCallback = () => {
                smc.gameMj.exitDesk()
                oops.gui.remove(UIID.createMjDesk)
                smc.dating.show()
            }
            if (resp.dissolveInitiator !== Player.shortId()) {
                // 不是自己发起的解散操作，提示房间已解散
                openDialogNotify("提示", "房间已解散", okCallback)
            } else {
                okCallback()
            }
        })

        // 新人加入
        comp.listenRoute(routersKey.deskJoinPush, (resp: pb.IDeskJoinPushResp) => {
            smc.gameMj.joinNewPlayer(resp.newPlayer)
            const index = smc.gameMj.getNodeIndexByShortId(resp.newPlayer.playerInfo.shortId)
            if (index === -1) {
                console.error("没有玩家信息", resp.newPlayer.playerInfo.shortId)
                return []
            }
            const comp = smc.gameMj.get(MjDeskView)
            const playerView = comp.getPlayerView()
            const playerInfo = resp.newPlayer.playerInfo
            const playerDeskInfo = resp.newPlayer.playerDeskInfo
            const shortId = Number(playerInfo.shortId)
            let isReady = playerDeskInfo.isReady
            if (smc.gameMj.isPlaying()) {
                // 游戏中，不显示准备好的手势
                isReady = false
            }
            playerView.initPlayerInfo(index, playerInfo.avatar, playerInfo.nickname,
                Number(playerInfo.ruby), shortId, isReady)
            comp.bindCounterByShortId(shortId, index)
        })

        // 游戏未开始, 有人退出
        comp.listenRoute(routersKey.deskExitPush, (resp: pb.IDeskExitPushResp) => {
            smc.gameMj.playerExit(resp.exitShortId)
            const comp = smc.gameMj.get(MjDeskView)
            const playerView = comp.getPlayerView()
            playerView.playerViewExit(resp.exitShortId)
        })

        // 有人准备好了
        comp.listenRoute(routersKey.deskReadyPush, (resp: pb.IDeskReadyPushResp) => {
            const comp = smc.gameMj.get(MjDeskView)
            const playerView = comp.getPlayerView()
            playerView.toggleReadyHandByShortId(resp.playerShortId, true)
        })

        this.onStart(comp)
    }

    // 游戏开始
    onStart(comp: MjListenRubyDeskComp) {
        comp.listenRoute(routersKey.mjStartDeskPush, (resp: pb.IMjStartDeskPushResp) => {
            console.log("start game", resp)
            // 第一次发的牌
            let firstTakeTile = null
            if (smc.gameMj.isMyTurn()) {
                firstTakeTile = getNewTakeTileFromDuanPai(resp.duanPai, Player.shortId())
            }
            // 清理数据
            smc.gameMj.updatePlayerCard(resp.duanPai)
            smc.gameMj.updateRoundInfo(resp.roundInfo)
            smc.gameMj.addStartDeskComp(firstTakeTile)
        })
    }
}